package com.ztz.mynewdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.ztz.mynewdemo.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublishDyActivity extends BaseActivity {
    public final static String TITLE = "title";
    public final static String CONTENTURL="contentUrl";
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.editTitle)
    EditText editTitle;
    @BindView(R.id.editContent)
    EditText editContent;
    @BindView(R.id.image_select)
    ImageView imageSelect;
    @BindView(R.id.btn_select)
    Button btnSelect;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.mProgess)
    ProgressBar mProgess;
    @BindView(R.id.ll)
    LinearLayout ll;
    private byte[] mImageBytes = null;
    String title;
    String contentUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_publish_dy);
        ButterKnife.bind(this);
        setToolbar("发布");
//        KeyBoardUtils.openKeyBoard(this,editTitle);
        title = getIntent().getStringExtra(TITLE);
        contentUrl=getIntent().getStringExtra(CONTENTURL);
        editTitle.setText(title);
    }

    @OnClick({R.id.btn_select, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_select:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 1);
                break;
            case R.id.btn_submit:
                if (editTitle.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (editContent.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mImageBytes == null) {
                    Toast.makeText(this, "请选择一张图片", Toast.LENGTH_SHORT).show();
                    return;
                }
                mProgess.setVisibility(View.VISIBLE);
                ll.setVisibility(View.INVISIBLE);
                AVObject product = new AVObject("Product");
                product.put("title", editTitle.getText().toString());
                product.put("content", editContent.getText().toString());
                product.put("contentUrl",contentUrl);
                product.put("owner", AVUser.getCurrentUser());
                product.put("image", new AVFile("productPic", mImageBytes));
                product.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            mProgess.setVisibility(View.GONE);
                            startActivity(new Intent(PublishDyActivity.this, DynaActivity.class));
                            PublishDyActivity.this.finish();
                        } else {
                            mProgess.setVisibility(View.GONE);
                            Toast.makeText(PublishDyActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            try {
                imageSelect.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()));
                mImageBytes = getBytes(getContentResolver().openInputStream(data.getData()));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public byte[] getBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buf = new byte[bufferSize];
        int len;
        while ((len = inputStream.read(buf)) != -1) {
            byteArrayOutputStream.write(buf, 0, len);
        }
        return byteArrayOutputStream.toByteArray();
    }

    @Override
    protected void onPause() {
        super.onPause();
        AVAnalytics.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AVAnalytics.onResume(this);
    }
}
